package com.spreadk.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author:Gan Linghao
 * @date:2020/12/9
 * @time:19:20
 * @description:no description
 */
@Aspect
@Component
public class AopAspect {

//    @Pointcut("execution(public * com.spreadk.demo.aop.PointDemo.getUser(..)) && args(param)")
//    public void getUserDemo(String param) {
//
//    }
@Pointcut("execution(public * com.spreadk.demo.mapper.impl.UserMapperImpl2.updateUser(java.lang.String,java.lang.Long))")
public void getUserDemo() {

}
//@Pointcut("execution(public * com.spreadk.demo.mapper.impl.UserMapperImpl2.updateUser(java.lang.String,java.lang.Long)) && (name,id)")
//public void getUserDemo(String name,String id) {
//
//}

    /**
     * 前置
     * @param param
     */
//    @Before("getUserDemo(param)")
//    public void beforeGetUser(String param) {
//        System.out.println("---------执行方法前，参数是: " + param);
//    }
//
//    /**
//     * 后置
//     * @param param
//     */
//    @After("getUserDemo(param)")
//    public void afterGetUser(String param) {
//        System.out.println("---------执行方法后，参数是: " + param);
//    }
//
//    /**
//     * 抛异常
//     * @param param
//     * @param ex
//     */
//    @AfterThrowing(pointcut = "getUserDemo(param)",throwing = "ex")
//    public void afterGetUserThrow(String param, Throwable ex) {
//        System.out.println("---------执行方法时抛出异常, 参数： " + param + "异常内容：" + ex);
//    }
//
//    /**
//     * 返回后运行
//     * @param param
//     * @param res
//     */
//    @AfterReturning(pointcut = "getUserDemo(param)",returning = "res")
//    public void afterGetUserReturn(String param, String res) {
//        System.out.println("---------执行方法返回后运行, 参数： " + param + "返回内容：" + res);
//    }

    /**
     * 环绕通知
     * @param joinPoint
     */
//    @Transactional(rollbackFor = Exception.class)
    @Around("getUserDemo()")
    public void afterRound(ProceedingJoinPoint joinPoint) throws Throwable {
        String param = (String) joinPoint.getArgs()[0];
        try {
            System.out.println("执行方法前：" + param);
           String res =  (String)joinPoint.proceed();
            System.out.println("执行方法后：" + res);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("执行抛出异常后：" + throwable);
            throw throwable;
        }
    }

}
